package com.srs.dao.cfg;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.cfg.Role;
import com.srs.model.cfg.Scope;
import com.srs.util.HibernateUtil;

public class ScopeDao {

	final static Logger logger = LoggerFactory.getLogger(ScopeDao.class);

	private List<Scope> scopeList;
	private Scope scope;

	public void delete(Integer scopeId) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			scope = (Scope) session.get(Scope.class, scopeId);
			session.delete(scope);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Scope> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			scopeList = session.createQuery("from Scope").list();
			return scopeList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}


	@SuppressWarnings("unchecked")
	public List<Scope> list(String scopesInGroup) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			if (scopesInGroup!=null && !scopesInGroup.equalsIgnoreCase("")){
				scopeList = session.createQuery("from Scope s where s.scopeId not in (" + scopesInGroup + ")").list();
			} else {
				scopeList = session.createQuery("from Scope").list();
			}
			return scopeList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	public Boolean nameIsUsed(String scopeName) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from Scope s where s.scopeName=:scopeName");
			q.setString("scopeName", scopeName);
			scopeList = q.list();
			if (scopeList.size()==0) {
				return false;
			}
			return true;
		} finally {
			session.close();
		}

	}		
	
	public Scope get(Integer scopeId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from Scope s where s.scopeId=:scopeId");
			q.setInteger("scopeId", scopeId);
			return (Scope) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public void insert(Scope scope) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(scope);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(Scope scope) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(scope);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

}
